package com.lannuokeji.biz.items.controller;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lannuokeji.base.AController;
import com.lannuokeji.base.facade.PageRequest;
import com.lannuokeji.biz.items.entity.Device;
import com.lannuokeji.biz.items.service.DeviceService;
import com.lannuokeji.biz.sys.entity.Advice;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 设备信息表 前端控制器
 * </p>
 *
 * @author lannuoTeck
 * @since 2018-03-19
 */
@Api(description="设备管理")
@RestController
@RequestMapping("/items/device")
public class DeviceController extends AController {

	@Autowired
	DeviceService deviceService;

	@ApiOperation(value = "关联查询分页列表")
	@GetMapping()
	public Object getList(PageRequest pageRequest, Device entity) {
		System.out.println("用户id：" + SecurityUtils.getSubject().getPrincipal());
		Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());
		page = deviceService.page(page, entity);
		return setSuccessContent(page);
	}
	
	@ApiOperation(value = "查询当前用户被允许借用的设备")
	@GetMapping("/allowMe")
	public Object getList2(PageRequest pageRequest) {
		String userId = getUser();
		Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());
		page = deviceService.selectPage(page, new EntityWrapper<Device>().like("roleids", userId+","));
		return setSuccessContent(page);
	}

	@ApiOperation(value = "添加设备")
	@PostMapping()
	public Object create(HttpServletRequest request, @RequestBody Device entity) {
		entity.setCreateTime(new Date());
		boolean ret = deviceService.insertAllColumn(entity);
		if (ret) {
			return setSuccessContent("添加成功！");
		} else {
			return setErrorContent("添加失败！");
		}
	}

	@ApiOperation(value = "根据id查询设备")
	@GetMapping("/{id}")
	public Object get(HttpServletRequest request,
			@ApiParam(value = "设备id", required = true) @PathVariable Integer id) {
		Device items = deviceService.selectById(id);
		return setSuccessContent(items);
	}

	@ApiOperation(value = "删除设备信息")
	@DeleteMapping("/{id}")
	public Object del(HttpServletRequest request,
			@ApiParam(value = "设备id", required = true) @PathVariable Integer id) {
		if (deviceService.deleteById(id)) {
			return setSuccessContent("删除成功");
		} else {
			return setErrorContent("删除失败");
		}
	}

	@ApiOperation(value = "修改设备信息")
	@PutMapping()
	public Object update(HttpServletRequest request, @RequestBody Device entity) {
		if (deviceService.updateById(entity)) {
			return setSuccessContent("修改成功！");
		} else {
			return setErrorContent("修改失败！");
		}
	}

	@ApiOperation(value = "获取设备允许借用的用户组")
	@GetMapping("/userGroups")
	public Object getUserGroups(HttpServletRequest request,
			@ApiParam(name = "id", value = "设备id", required = true) @RequestParam(value = "id", required = true) Integer id) {
		Device items = deviceService.selectById(id);
		return items;
	}

	@ApiOperation(value = "修改设备被允许使用的用户组，多选（移植出来？）")
	@PutMapping("/userGroups")
	public Object updateUse(HttpServletRequest request,
			@ApiParam(name = "roleids", value = "设备使用权限分配ids", required = true) @RequestParam(value = "roleids", required = true) String roleids,
			@ApiParam(name = "id", value = "设备id", required = true) @RequestParam(value = "id", required = true) Integer id) {
//		Integer i = deviceService.updateById(entity);
//		System.out.println(i);
		if (1 == 1) {
			return setSuccessContent("修改成功！");
		} else {
			return setErrorContent("修改失败！");
		}
	}
}

